<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/12
 * Time: 12:00
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\User;
use common\models\Http;
use wap\models\Service\WechatModel;

class WxHeadService {
    public static function get($head, $userInfo){
        $options = array(
            'token'=>\Yii::$app->params['token'], //填写你设定的key
            'appid'=> \Yii::$app->params['appID'],
            'appsecret'=>\Yii::$app->params['appSecret']
        );
        $weObj = new WechatModel( $options);
        $getInfo = $weObj->getUserInfo($userInfo['openID']);
        if(!$getInfo || empty($getInfo['subscribe']))
            return '';
        $user = User::findOne($userInfo['userID']);
        if(!$user)
            return '';
        $user->avatar = $getInfo['headimgurl'];
        /*   $user->sex = $getInfo['sex'];
           $user->country = $getInfo['country'];
           $user->province = $getInfo['province'];
           $user->city = $getInfo['city'];*/
        $result = $user->save();
        if(!$result){
            return '';
        }
        $userInfo['head'] = $getInfo['headimgurl'];
        Http::session('user', $userInfo);
        return $getInfo['headimgurl'];
    }

}